<?php
/**
 *  输出
 * User: shouxue
 * Date: 2017/9/25
 * Time: 13:30
 */

//引入输出
class Response{

    protected static $tramsform = null;
    protected static $type      = '';
    protected static $data      = '';
    protected static $isExit    = true;

    //输出json
    public  function Json($code,$msg, $data = '',$type = 'json')
    {

        $list = array(
            'code' => $code,
            'msg'  => $msg,
            'time' => time(),
            'data' => $data,
        );

        //本身就是对象 无需处理
        if(isset($data['code']) && isset($data['time'])){
             $list = $data;
        }

        self::send($list,$type);
    }


    //返回
    public  function Min($code,$msg, $data = '',$type = 'json')
    {
        $list= array(
            'code' => $code,
            'msg'  => $msg,
            'time' => time(),
            'data' => $data,
        );

        return  $list;
    }


    //输出
    public static function send($data = '', $type = '', $return = false)
    {
        $type = strtolower($type ? $type : self::$type);

        $headers = array(
            'json'   => 'application/json',
            'xml'    => 'text/xml',
            'html'   => 'text/html',
            'jsonp'  => 'application/javascript',
            'script' => 'application/javascript',
            'text'   => 'text/plain',
        );

        if (!headers_sent() && isset($headers[$type])) {
            header('Content-Type:' . $headers[$type] . '; charset=utf-8');
        }

        $data = $data ? $data : self::$data;
        if (is_callable(self::$tramsform)) {

             $data = call_user_func_array(self::$tramsform, array($data));

        } else {

             $data =  self::output($data);
             
        }

        if ($return) {
            return $data;
        }

        echo $data;
        self::isExit() && exit();
    }

    public static function tramsform($callback)
    {
        self::$tramsform = $callback;
    }

    public static function type($type = null)
    {
        if (is_null($type)) {
            return self::$type ? self::$type : 'json';
        }
        self::$type = $type;
    }

    public static function data($data)
    {
        self::$data = $data;
    }

    public static function isExit($exit = null)
    {
        if (is_null($exit)) {
            return self::$isExit;
        }
        self::$isExit = (boolean) $exit;
    }


    /**
     * 处理数据
     * @access protected
     * @param mixed $data 要处理的数据
     * @return mixed
     * @throws \Exception
     */
    public static  function output($data)
    {
        try {
            // 返回JSON数据格式到客户端 包含状态信息
            $data = self::json_encode($data);

            if ($data === false) {
                throw new InvalidArgumentException(json_last_error_msg());
            }

            return $data;
        } catch (Exception $e) {
            if ($e->getPrevious()) {
                throw $e->getPrevious();
            }
            throw $e;
        }
    }

    /**
     * 对数组变量进行 JSON 编码
     * @param mixed array 待编码的 array (除了resource 类型之外，可以为任何数据类型，该函数只能接受 UTF-8 编码的数据)
     * @return string (返回 array 值的 JSON 形式)
     */
    public static function json_encode($array)
    {
        if(version_compare(PHP_VERSION,'5.4.0','<')){
            $str = json_encode($array);
            $str = preg_replace_callback("#\\\u([0-9a-f]{4})#i",function($matchs){
                return iconv('UCS-2BE', 'UTF-8', pack('H4', $matchs[1]));
            },$str);
            return $str;
        }else{
            return json_encode($array, JSON_UNESCAPED_UNICODE);
        }
    }


    public function doSomething()
    {
        echo __METHOD__;
    }
}


